﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameBaseCode;
using GLWrapper;

namespace plane_game
{
    class Plane
    {
        public Vector3 dir;
        private Vector3 up;
        public Vector3 position;
        private Matrix3 frame;
        public ObjMesh mesh;
        public float speed = 0.0f;
        float hang = 0.0f;
        float vang = 0.0f;
        Heightmap heightmapTerrain;

        public Plane(Heightmap heightmapTerrain)
        {
            dir = new Vector3(0, 0, 1);
            up = new Vector3(0, 1, 0);
            position = new Vector3(0, 0, 0);
            frame = new Matrix3();
            this.heightmapTerrain = heightmapTerrain;
        }

        public void move(double dt)
        {
            position += speed * dir * (float)dt;
            //Vector3 left = Vector3.CrossProduct(dir, up);
            //frame.set(left, up, dir);
        }

        public void update()
        {
            //bool hit = heightmapTerrain.intersectAtPoint(position.x, position.y, position.z);
            //if (isec.isIntersection)
            //{
            //    // smoothly update normal
            //    currNormal.interpolateToSelf(isec.normal, 0.25f);
            //    // move bot slightly above terrain
            //    Vec3D newPos = isec.pos.add(0, 10, 0);
            //    pos.interpolateToSelf(newPos, 0.25f);
            //}

        }

        public void draw()
        {
            //Vector3 left = Vector3.CrossProduct(dir, up);
            //frame.set(left, up, dir);
            
            //frame = Matrix3.ang2mat(hang, vang);
            //dir.set(frame[6], frame[7], frame[8]);

            //Vector3 side = Vector3.CrossProduct(dir, up);

            //Vector3 dirVec = new Vector3(hang, vang, 0);
            //frame = Matrix3.initRotationMatrix(dirVec, hang);
            //frame = Matrix3.mul(tform2, frame);

            frame.setIdentity();
            frame.rotateX(-vang);
            frame.rotateY(-hang);
            //frame.rotateZ(hang);
            dir.set(frame[6], frame[7], frame[8]);

            //frame.setIdentity();
            //frame.rotateX(vang);
            //frame.rotateY(hang);
            //frame.rotateZ(hang);

            GL.glColor3f(1, 1, 1);
            GL.glDisable(GL.GL_LIGHTING);
            GL.glEnable(GL.GL_TEXTURE_2D);

            GL.glPushMatrix();
            
            GL.glTranslate(position);
            GL.glMultMatrix(frame);
            GL.glRotatef(90, 1, 0, 0);
            GL.glRotatef(180, 0, 1, 0);
            mesh.Render();
            GL.glPopMatrix();

            GL.glDisable(GL.GL_TEXTURE_2D);

        }

        public void rotateX(float planeRotX)
        {
            //dir.rotateX(planeRotX);
            //up.rotateX(planeRotX);
            vang += planeRotX;
        }

        public void rotateY(float planeRotY)
        {
            //dir.rotateY(planeRotY);
            //up.rotateY(planeRotY);
            hang += planeRotY;
        }

        public void rotateZ(float planeRotZ)
        {
            //dir.rotateZ(planeRotZ);
            //up.rotateY(planeRotY);
            
        }


        public Vector3 getCameraOffset()
        {
            frame.setIdentity();
            frame.rotateX(-vang);
            frame.rotateY(-hang);
            return new Vector3(frame[6], frame[7], frame[8]) * -65.0f;

        }
    }
}
